package com.wfm.eduservice.controller;


import com.wfm.commonutils.R;
import com.wfm.eduservice.entity.EduChapter;
import com.wfm.eduservice.entity.chapter.ChapterVo;
import com.wfm.eduservice.service.EduChapterService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wfm
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {
    private final EduChapterService chapterService;

    @Autowired
    public EduChapterController(EduChapterService chapterService) {
        this.chapterService = chapterService;
    }

    @ApiOperation(value = "嵌套章节数据列表")
    @GetMapping("nestedList/{courseId}")
    public R nestedListByCourseId(
            @ApiParam(name = "courseId", value = "课程ID", required = true)
            @PathVariable String courseId){
        List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
        return R.ok().data("items", chapterVoList);
    }
    @ApiOperation(value = "新增章节")
    @PostMapping("addChapter")
    public R save(
            @ApiParam(name = "chapterVo", value = "章节对象", required = true)
            @RequestBody EduChapter chapter){
        chapterService.save(chapter);
        return R.ok();
    }
    //根据id查chapter
    @GetMapping("getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId){
        EduChapter chapter = chapterService.getById(chapterId);
        return R.ok().data("chapter",chapter);
    }
    //根据id修改chapter
    @PostMapping("updateChapter")
    public R updateCapter(@RequestBody EduChapter chapter){
        chapterService.updateById(chapter);
        return R.ok();
    }
    //删除章节，比较特殊，有小节，两种处理方式
    @DeleteMapping("deleteChapter/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        boolean b =chapterService.deleteChapter(chapterId);
        if(b){
            return R.ok();
        }else{
            return R.error();
        }
    }

//    @ApiOperation(value = "根据ID删除章节")
//    @DeleteMapping("{id}")
//    public R removeById(
//            @ApiParam(name = "id", value = "章节ID", required = true)
//            @PathVariable String id){
//        boolean result = chapterService.removeChapterById(id);
//        if(result){
//            return R.ok();
//        }else{
//            return R.error().message("删除失败");
//        }
//    }
//    @ApiOperation(value = "根据ID修改章节")
//    @PutMapping("{id}")
//    public R updateById(
//            @ApiParam(name = "id", value = "章节ID", required = true)
//            @PathVariable String id,
//            @ApiParam(name = "chapter", value = "章节对象", required = true)
//            @RequestBody EduChapter chapter){
//        chapter.setId(id);
//        chapterService.updateById(chapter);
//        return R.ok();
//    }
}

